<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2.纹理叠加效果</title>
    <style>
        canvas{
            border:1px solid #aaa;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="0" height="0"></canvas>

    <script type="module">

        import { loadImage, getImageData, traverse, gaussianBlur, getPixel } from "/common/lib/utils.js"
        import { transformColor, multiply, grayscale, brightness, saturate, contrast } from "/common/lib/color-matrix.js"

        const canvas = document.querySelector('canvas'); 
        const context = canvas.getContext('2d');

        (async function (){
            // 异步加载图片
            const img = await loadImage('https://p2.ssl.qhimg.com/d/inn/4b7e384c55dc/girl1.jpg');
            const sunlight = await loadImage('assets/sunlight.png');
            
            // 获取图片剪裁区的数据 imgData（包含了图片的所有像素数据）
            const imageData = getImageData(img);
            const texture = getImageData(sunlight);

            // 循环遍历 imageData 数据，并处理
            traverse(imageData,({r, g, b, a, index}) => { 
                // 获取要重叠图片的颜色色阶值
                const texColor = getPixel(texture, index);
                return transformColor(
                        [r, g, b, a], 
                        brightness(1 + 0.7 * texColor[3]), 
                        saturate(2 - texColor[3])
                    ); 
            });
            canvas.width = imageData.width;
            canvas.height = imageData.height;
            context.putImageData(imageData, 0, 0);
        })();
    </script>
</body>
</html>